package com.example.luke.shengdangxiaoattendance;

import android.os.Bundle;
import android.widget.TextView;

import com.example.luke.shengdangxiaoattendance.Common.CommonActivity;
import com.example.luke.shengdangxiaoattendance.Model.App;
import com.example.luke.shengdangxiaoattendance.Model.StudentAttendance;
import com.example.luke.shengdangxiaoattendance.Util.GlobalFunction;

import java.sql.Time;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 31/05/2017.
 */

public class DetailActivity extends CommonActivity {

    @InjectView(R.id.totalStudent)TextView totStudent;
    @InjectView(R.id.presentStudent)TextView preStudent;
    @InjectView(R.id.absentStudent)TextView abStudent;

    @InjectView(R.id.weekDay)TextView weekDay;
    @InjectView(R.id.currentTime)TextView currentTime;

    @InjectView(R.id.todayDate)TextView todayDate;

    @InjectView(R.id.name)TextView name;
    @InjectView(R.id.gender)TextView gender;
    @InjectView(R.id.className)TextView className;
    @InjectView(R.id.name2)TextView  name2;

     @InjectView(R.id.attendStat)TextView attendStat;


    private boolean studentAttend=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.center_detail_container);





        try {


            initView();


        }catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());
        }

    }

    @Override
    public void initView() {
        super.initView();

        ButterKnife.inject(this);
        currentTime.setText(GlobalFunction.getTimeWithFomat(new Date(), "HH:mm:ss"));
        todayDate.setText(GlobalFunction.getTimeWithFomat(new Date(), "MM月dd日"));

//        climate.setText(App.getInstance().climate);
//        temperature.setText(App.getInstance().temperature);
//        airDirection.setText(App.getInstance().airDirection);

        weekDay.setText(GlobalFunction.getWeekDay());
        name.setText(App.getInstance().selectedStudent.name);

        gender.setText(App.getInstance().selectedStudent.gender);

        className.setText(App.getInstance().selectedStudent.className);
        totStudent.setText(App.getInstance().totalStudent);

       name2.setText(App.getInstance().currentCourse.name2);

     studentAttend=StudentAttendance.checkStudentAttend(this,App.getInstance().selectedStudent.className
             ,GlobalFunction.getTimeWithFomat(new Date(),"yyyy-MM-dd"),App.getInstance().selectedStudent.employeeID,App.getInstance().currentCourse.Time);

        int curPreCount=Integer.valueOf(App.getInstance().preStudent);

      if (studentAttend){

          attendStat.setText("已签到！");
      }else {
          if (checkIsLate()){
              attendStat.setText("已迟到！");
          }
         curPreCount=curPreCount+1;//

      }

        reloadAttendCount(curPreCount);

       backToMain();


    }
    private  void reloadAttendCount(int curPreCount){


        preStudent.setText(curPreCount+"");
        Integer abStudentCount=Integer.valueOf(App.getInstance().totalStudent)-curPreCount;

        abStudent.setText(abStudentCount+"");
    }


    public  void  backToMain(){

        Timer timer=new Timer();
        timer.schedule(new Task(),2500);
    }

    private  class Task extends TimerTask{
        @Override
        public void run() {

        saveAttendance();
        finish();

        }
    }

    private void saveAttendance(){

     if (!studentAttend){
        StudentAttendance studentAttendance=new StudentAttendance();
        studentAttendance.name=App.getInstance().selectedStudent.name;
        studentAttendance.className=App.getInstance().selectedStudent.className;
        studentAttendance.employeeID=App.getInstance().selectedStudent.employeeID;
        studentAttendance.attendanceDate=GlobalFunction.getTimeWithFomat(new Date(),"yyyy-MM-dd");
        studentAttendance.attendanceTime=GlobalFunction.getTimeWithFomat(new Date(),"HH:mm:ss");
        studentAttendance.classTime=App.getInstance().currentCourse.Time;

        StudentAttendance.saveAttendance(this,studentAttendance);
    }



    }

    private  boolean checkIsLate(){

        return new Date().after(App.getInstance().currentCourse.getStartTime());


    }
}
